package com.ssii.www.myproject.web.exception;

import com.ssii.www.myproject.common.util.LogUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {
  private static final Logger logger = LogUtils.getLogger();

  @ExceptionHandler
  @ResponseBody
  public Map<String, Object> returnData(Exception e) throws Exception {
//    /** 400 - Bad Request
//     */
//    ResponseStatus(HttpStatus.BAD_REQUEST)
//    ExceptionHandler(HttpMessageNotReadableException.class)
//    public ServiceResponse handleHttpMessageNotReadableException (HttpMessageNotReadableException e){
//      logger.error("参数解析失败", e);
//      return ServiceResponseHandle.failed("could_not_read_json");
//    }
//
//    /**
//     * 405 - Method Not Allowed
//     */
//    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
//    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
//    public ServiceResponse handleHttpRequestMethodNotSupportedException (HttpRequestMethodNotSupportedException e){
//      logger.error("不支持当前请求方法", e);
//      return ServiceResponseHandle.failed("request_method_not_supported");
//    }
//
//    /**
//     * 415 - Unsupported Media Type
//     */
//    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
//    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
//    public ServiceResponse handleHttpMediaTypeNotSupportedException (Exception e){
//      logger.error("不支持当前媒体类型", e);
//      return ServiceResponseHandle.failed("content_type_not_supported");
//    }
//
//    /**
//     * 500 - Internal Server Error
//     */
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    @ExceptionHandler(Exception.class)
//    public ServiceResponse handleException (Exception e){
//      if (e instanceof BusinessException) {
//        return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
//      }
//
//      logger.error("服务运行异常", e);
//      e.printStackTrace();
//      return ServiceResponseHandle.failed("server_error");
//    }

    Map<String, Object> map = new HashMap<>();
    //map.put("e",e);
    if (e instanceof NullPointerException) {
      map.put("msg", "空指针异常");
      map.put("error-code", -101);
      logger.error(e.getMessage(), e);
    }
    if (e instanceof UnauthorizedException) {
      map.put("msg", "没有权限");
      map.put("error-code", -103);
    } else {
      map.put("msg", e);
      map.put("error-code", -102);
      logger.error(e.getMessage(), e);
    }
    return map;
  }

  //可以写一个方法来捕获自定义的异常
  //@ExceptionHandler("自定义异常.class")
}
